package yunwo.cn.yzstation.model;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.IOrderModel;
import yunwo.cn.yzstation.api.okapi.BaoFeiCallBack;
import yunwo.cn.yzstation.api.okapi.EndOrganCallBack;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.api.okapi.YunFeiCallBack;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.BaoFeiBean;
import yunwo.cn.yzstation.bean.EndOrganbean;
import yunwo.cn.yzstation.bean.OpenBean;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingInfo;
import yunwo.cn.yzstation.bean.PopupBean;
import yunwo.cn.yzstation.bean.Waybillbean;
import yunwo.cn.yzstation.bean.YunfeiBean;
import yunwo.cn.yzstation.config.GoodsSP;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.view.activity.LoginActivity;
import yunwo.cn.yzstation.view.activity.OrderActivity;

/**
 * Created by lao on 2017/9/11.
 */
public class OrderModel {

    OrderActivity orderActivity;
    IOrderModel iOrderModel;
    private SharedPreferences feiyong_sp;
    private ProgressDialog mMyDialog;

    public OrderModel(OrderActivity orderActivity, IOrderModel iOrderModel) {
        this.orderActivity = orderActivity;
        this.iOrderModel = iOrderModel;
        feiyong_sp = orderActivity.getSharedPreferences("feiyong", Context.MODE_PRIVATE);
    }

    public void getXiaoFei(String s) {

/*
        Map<String, Object> map = new HashMap<>();
        map.put("feeWay", feiyong_sp.getString("xiaofei", ""));
        map.put("standardVal", s);

        HttpManager.hpptPostString3(MyConstants.Xiaofei, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {

            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });*/
    }

    /*new BaoFeiCallBack(BaoFeiBean.class, orderActivity, false) {
                @Override
                public void myError(Call call, Exception e, int id) {
                    super.myError(call, e, id);
                }

                @Override
                public void onResponse(BaoFeiBean response, int id) {
                    if (response.getCode() == 200) {
                        iOrderModel.getXiaoFeiSuccess(response.getData());
                    }
                }
            }*/
    //目的站
    public void getEndOrgan() {
        HashMap<String, String> map = new HashMap<>();
        map.put("params", Yzapp.getInstance().getBeginOrgId());
        HttpManager.hpptPostString(MyConstants.EndOrgan, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!result.equals("")) {
                        List<EndOrganbean.DataBean> dataBeanList = new Gson().fromJson(result, new TypeToken<List<EndOrganbean.DataBean>>() {
                        }.getType());
                        iOrderModel.getEndOrganSuccess(dataBeanList);
                        Log.d("xxxxxxxxx", dataBeanList.size() + "");

                    }
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });

    }

    //计算运费 按重量
    public void getYunFei(String s, String type) {
  /*      if ("1".equals(type)) {
            Map<String, Object> map = new HashMap<>();
            map.put("beginOrganId", Yzapp.getInstance().getBeginOrgId());
            map.put("endOrganId", GoodsSP.getInstance().getGoodsShare().getString("endOrganId", ""));
            map.put("feeWay", feiyong_sp.getString("yunfei_zhongliang", ""));
            map.put("speedUnit", "01");  //默认快件
            map.put("standardVal", s);

            HttpManager.hpptPostString3(MyConstants.Yunfei, map).execute(new YunFeiCallBack(YunfeiBean.class, orderActivity, false) {
                @Override
                public void myError(Call call, Exception e, int id) {
                    Log.d("zxcvb", e.toString());
                }

                @Override
                public void onResponse(YunfeiBean response, int id) {
                    if (response.getCode() == 200) {
                        iOrderModel.getYunFeiSuccsee(response);

                    }
                }
            });

        } else if ("2".equals(type)) {
            Map<String, Object> map = new HashMap<>();
            map.put("beginOrganId", Yzapp.getInstance().getBeginOrgId());
            map.put("endOrganId", GoodsSP.getInstance().getGoodsShare().getString("endOrganId", ""));
            map.put("feeWay", feiyong_sp.getString("yunfei_tiji", ""));
            map.put("speedUnit", "01");  //默认快件
            map.put("standardVal", s);

            HttpManager.hpptPostString3(MyConstants.Yunfei, map).execute(new YunFeiCallBack(YunfeiBean.class, orderActivity, false) {
                @Override
                public void myError(Call call, Exception e, int id) {
                    Log.d("zxcvb", e.toString());
                }

                @Override
                public void onResponse(YunfeiBean response, int id) {
                    if (response.getCode() == 200) {
                        iOrderModel.getYunFeiSuccsee(response);

                    }
                }
            });
        }*/

    }

    public void submit(final String type, final StaTrackingInfo orderInfoBean) {


        HttpManager.hpptPostString2(MyConstants.OpenOrder, orderInfoBean).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {

                if (code == 200) {
                    iOrderModel.OpenOrderSuccess(type, orderInfoBean);
                } else {
                    iOrderModel.OpenOrderfailure(code, msg);
                }


            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(orderActivity, "开单失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBefore(Request request, int id) {
                if (mMyDialog == null) {
                    mMyDialog = ProgressDialog.show(orderActivity, null, "请稍等...");
                    mMyDialog.setCancelable(false);
                    mMyDialog.show();
                }
            }

            @Override
            public void onAfter(int id) {
                if (mMyDialog != null && mMyDialog.isShowing()) {
                    mMyDialog.dismiss();
                }
            }
        });


 /*       final Map<String, String> map = new HashMap<>();
        setvalue(map);
        HttpManager.hpptPostString(MyConstants.OpenOrder, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                Log.d("qqqqqqqqqq", code + msg );
                if (result.length() > 0) {
                    if (code == 200) {
                        OpenBean openBean = new Gson().fromJson(result, OpenBean.class);
                        Log.d("qqqqqqqqqq", code + msg + openBean.getData());
                        String data = openBean.getData();
                    }

                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Log.d("qqqqqqqqqq", e.toString());
            }
        });*/
    }

    private void setvalue(Map<String, String> map) {
        map.put("trackingId", "");//运单id
        map.put("trackingNumber", GoodsSP.getInstance().getGoodsShare().getString("trackingNumber", ""));  //运单号
        map.put("consigneeName", GoodsSP.getInstance().getGoodsShare().getString("consigneeName", ""));//收货人
        map.put("consigneePhone", GoodsSP.getInstance().getGoodsShare().getString("consigneePhone", "")); //收货人电话
        map.put("consigneeAddress", GoodsSP.getInstance().getGoodsShare().getString("consigneeAddress", ""));//收货人地址
        map.put("consignorName", GoodsSP.getInstance().getGoodsShare().getString("consignorName", ""));// 发货人
        map.put("consignorPhone", GoodsSP.getInstance().getGoodsShare().getString("consignorPhone", ""));//发货人电话
        map.put("consignorAddress", GoodsSP.getInstance().getGoodsShare().getString("consignorAddress", ""));//发货人地址
        map.put("receiptPay", GoodsSP.getInstance().getGoodsShare().getString("receiptPay", "否"));//回单付

        map.put("receiptRequire", GoodsSP.getInstance().getGoodsShare().getString("receiptRequire", ""));//回单要求
        map.put("bank", GoodsSP.getInstance().getGoodsShare().getString("bank", ""));//开户行
        map.put("bankName", GoodsSP.getInstance().getGoodsShare().getString("bankName", ""));//开户名
        map.put("bankAccount", GoodsSP.getInstance().getGoodsShare().getString("bankAccount", ""));//开户账号
        map.put("pullPackWorker", GoodsSP.getInstance().getGoodsShare().getString("pullPackWorker", ""));//拉包工
        map.put("pullPackWorkerPhone", GoodsSP.getInstance().getGoodsShare().getString("pullPackWorkerPhone", ""));//拉包工电话


        map.put("beginOrganId", Yzapp.getInstance().getBeginOrgId());  //始发站id
        map.put("beginOrganName", Yzapp.getInstance().getgetBeginOrgName());//始发站名
        map.put("transferOrganId", GoodsSP.getInstance().getGoodsShare().getString("transferOrganId", ""));//中转站id
        map.put("transferOrganName", GoodsSP.getInstance().getGoodsShare().getString("transferOrganName", ""));//中转站名称

        Log.d("xxxxxxxxx", GoodsSP.getInstance().getGoodsShare().getString("endOrganId", "") + GoodsSP.getInstance().getGoodsShare().getString("endOgranName", ""));
        map.put("endOrganId", GoodsSP.getInstance().getGoodsShare().getString("endOrganId", ""));//目的站ID
        map.put("endOrganName", GoodsSP.getInstance().getGoodsShare().getString("endOrganName", ""));//目的站名

        map.put("goodsSelfNumber", GoodsSP.getInstance().getGoodsShare().getString("goodsSelfNumber", ""));//自编号

        map.put("productId", GoodsSP.getInstance().getGoodsShare().getString("", ""));//品名ID
        map.put("productName", GoodsSP.getInstance().getGoodsShare().getString("productName", ""));//品名

        map.put("packId", GoodsSP.getInstance().getGoodsShare().getString("", ""));//包装id

        map.put("packName", GoodsSP.getInstance().getGoodsShare().getString("packName", ""));//包装名

        map.put("goodsNumber", GoodsSP.getInstance().getGoodsShare().getString("goodsNumber", ""));//件数
        map.put("weight", GoodsSP.getInstance().getGoodsShare().getString("weight", ""));//重量
        map.put("volume", GoodsSP.getInstance().getGoodsShare().getString("volume", ""));//体积

        map.put("payMeansId", GoodsSP.getInstance().getGoodsShare().getString("payMeansId", ""));//付款方式id
        map.put("payMeansName", GoodsSP.getInstance().getGoodsShare().getString("payMeansName", ""));//付款方式

        map.put("deliveryId", GoodsSP.getInstance().getGoodsShare().getString("deliveryId", ""));//交接方式id
        map.put("deliveryName", GoodsSP.getInstance().getGoodsShare().getString("deliveryName", ""));//交接方式
        map.put("remarks", GoodsSP.getInstance().getGoodsShare().getString("remarks", ""));//备注

        map.put("freight", GoodsSP.getInstance().getGoodsShare().getString("freight", ""));//运费
        map.put("reputation", GoodsSP.getInstance().getGoodsShare().getString("reputation", ""));//声价
        map.put("rebate", GoodsSP.getInstance().getGoodsShare().getString("rebate", ""));//回扣
        map.put("premium", GoodsSP.getInstance().getGoodsShare().getString("premium", ""));//保费
        map.put("advanceTransferCharges", GoodsSP.getInstance().getGoodsShare().getString("advanceTransferCharges", ""));//垫付中转费
        map.put("otherCharges", GoodsSP.getInstance().getGoodsShare().getString("otherCharges", ""));//其它费用
        map.put("deliveryCharges", GoodsSP.getInstance().getGoodsShare().getString("deliveryCharges", ""));//送货费
        map.put("serviceCharges", GoodsSP.getInstance().getGoodsShare().getString("serviceCharges", ""));//服务费
        map.put("collectMoney", GoodsSP.getInstance().getGoodsShare().getString("collectMoney", ""));//代收货款
        map.put("procurationCharges", GoodsSP.getInstance().getGoodsShare().getString("procurationCharges", ""));//贷款手续费
        map.put("upstairsCharges", GoodsSP.getInstance().getGoodsShare().getString("upstairsCharges", ""));//上楼费
        map.put("floorCharges", GoodsSP.getInstance().getGoodsShare().getString("floorCharges", ""));//落地费
        map.put("customsCharges", GoodsSP.getInstance().getGoodsShare().getString("customsCharges", ""));//报关费

    }

    public void requestPopupCon(String type, String data) {
        Map<String, Object> map = new HashMap<>();
        switch (type) {
            case "1":
                map.put("name", data);
                break;
            case "2":
                map.put("phone", data);
                break;
            case "3":
                map.put("address", data);
                break;
        }
        HttpManager.hpptPostString3(MyConstants.Searchconsignee, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!result.equals("")) {

                        List<PopupBean.DataBean> PopupBean = new Gson().fromJson(result, new TypeToken<List<PopupBean.DataBean>>() {
                        }.getType());
                        iOrderModel.requestPopupConSuccess(PopupBean);

                    }
                } else {

                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });
    }

    public void getWayBill() {

        RequestCall build = OkHttpUtils.post().url(MyConstants.Waybill)
                .addHeader("Authorization", Yzapp.getInstance().getAuthorization())
                .build();
        build.execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {

                    if (!result.equals("\"\"") && !result.equals("")) {
                        Waybillbean.DataBean dataBean = new Gson().fromJson(result, Waybillbean.DataBean.class);
//                    iorderActivityModel.getWayBillSuccess(dataBean);
                        iOrderModel.getWayBillSuccess(dataBean);

                    }
                }else {
                    Toast.makeText(orderActivity, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(orderActivity, " 获取运单号失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBefore(Request request, int id) {
                if (mMyDialog == null) {
                    mMyDialog = ProgressDialog.show(orderActivity, null, "获取运单号中...");
                    mMyDialog.setCancelable(false);
                    mMyDialog.show();
                }

            }

            @Override
            public void onAfter(int id) {
                if (mMyDialog != null && mMyDialog.isShowing()) {
                    mMyDialog.dismiss();
                }
            }
        });
    }
}
/*new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.d("kkkkkkk", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.d("kkkkkkk", response);
            }
        }*/